package com.sr.examination.exam.exercise.category;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.CallSuper;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RatingBar;
import android.widget.TextView;

import com.sr.examination.R;
import com.tl.commonlibrary.picture.ImageGetterThread;
import com.tl.commonlibrary.ui.BaseFragment;
import com.tl.commonlibrary.ui.beans.ExerciseBean;
import com.tl.commonlibrary.ui.beans.ExerciseListBean;
import com.tl.commonlibrary.ui.beans.ExerciseOption;
import com.tl.commonlibrary.ui.widget.NumberEditText;

import java.util.List;

/**
 * Created by jiangzhongshan on 18/2/3.
 * 题目基础Fragment，下辖（选择题Choice和主观题Subjective)
 */

public abstract class ExerciseFragment extends BaseFragment {
    private static final int IMG_TAG_DESCRIPTION = 1;
    private static final int IMG_TAG_RIGHT_ANSWER = 2;
    private static final int IMG_TAG_ANALYSIS = 3;
    protected int exerciseType = ExerciseBean.TYPE_UNKOWN;//题目类型
    protected int examMethod = ExerciseListBean.METHOD_PRACTICE;//考试类型
    protected Handler imgHandler = new Handler(new Handler.Callback() {

        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.arg1) {
                case IMG_TAG_DESCRIPTION:
                    descriptionTView.setText((CharSequence) msg.obj);
                    break;
                case IMG_TAG_RIGHT_ANSWER:
                    answerTView.setText((CharSequence) msg.obj);
                    break;
                case IMG_TAG_ANALYSIS:
                    analysisTView.setText((CharSequence) msg.obj);
                    break;
            }
            return false;
        }
    });

    public static Bundle getCommomArguments(ExerciseBean exerciseBean) {
        Bundle args = new Bundle();
        args.putSerializable("exercise", exerciseBean);
        return args;
    }

    protected ExerciseBean exerciseBean;

    private TextView descriptionTView, answerTView, analysisTView;
    protected TextView myAnswerTView;
    protected RatingBar ratingBar;
    protected View analysisLayout;

    @Override
    public void onCreate(Bundle savedInstanceState) {
//        exerciseBean = (ExerciseBean) getArguments().getSerializable("exercise");
        super.onCreate(savedInstanceState);
    }

    public void setExerciseBean(ExerciseBean exerciseBean) {
        this.exerciseBean = exerciseBean;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        analysisLayout = root.findViewById(R.id.analysisLayout);
        descriptionTView = root.findViewById(R.id.descriptionTView);
        answerTView = root.findViewById(R.id.answerTView);
        myAnswerTView = root.findViewById(R.id.myAnswerTView);
        analysisTView = root.findViewById(R.id.analysisTView);
        ratingBar = root.findViewById(R.id.ratingBar);
        ratingBar.setRating(exerciseBean.getDifficulty());
        onExerciseDisplayModeChanged(exerciseBean.getDisplayMode());
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    public int getExamMethod() {
        return examMethod;
    }

    public void setExamMethod(int examMethod) {
        this.examMethod = examMethod;
    }

    @Override
    public void lazyLoad() {
        super.lazyLoad();
    }

    /**
     * 禁止修改答案
     */
    @CallSuper
    public boolean forbiddenAnswer() {
        exerciseBean.setAnswered(true);
        return true;
    }

    /**
     * 进入解析模式
     */
    public void toExerciseModeAnalysis() {
        saveExerciseInstanceState();
        onExerciseDisplayModeChanged(ExerciseBean.DISPLAY_MODE_ANALYSIS);
    }

    /**
     * 当试题模式改变时调用：练习，考试
     *
     * @param displayMode
     */
    @CallSuper
    protected void onExerciseDisplayModeChanged(int displayMode) {
//        if (exerciseBean.getDisplayMode() == displayMode) {
//            return;
//        }
        exerciseBean.setDisplayMode(displayMode);
        switch (displayMode) {
            case ExerciseBean.DISPLAY_MODE_ANALYSIS:
                onExerciseAnalysisModeCalled();
                break;
            case ExerciseBean.DISPLAY_MODE_ANSWERING:
                onExerciseAnsweringModeCalled();
                break;
            case ExerciseBean.DISPLAY_MODE_ANALYSIS_MARK:
                onExerciseAnalysisMarkModeCalled();
                break;
        }
    }


    /**
     * 正在答题，答题模式
     */
    @CallSuper
    protected void onExerciseAnsweringModeCalled() {
        if (analysisLayout != null) {
            analysisLayout.setVisibility(View.GONE);
        }
    }

    /**
     * 正在查看解析
     */
    @CallSuper
    protected void onExerciseAnalysisModeCalled() {
        //查看解析之后，当前题目不可再次修改答案了
        forbiddenAnswer();
        if (analysisLayout != null) {
            analysisLayout.setVisibility(View.VISIBLE);
        }
        if (myAnswerTView != null) {
            myAnswerTView.setText(exerciseBean.getUserAnswer());
        }
        setAnalysis(exerciseBean.getAnalysis());
    }

    /**
     * 正在查看解析并可自评分
     */
    @CallSuper
    protected void onExerciseAnalysisMarkModeCalled() {
        onExerciseAnalysisModeCalled();
    }

    /**
     * 当需要保存习题某些状态时调用，如交卷，离开当前试题
     */
    @CallSuper
    public void saveExerciseInstanceState() {
    }

    protected abstract void onClickOption(List<ExerciseOption> options);


    /**
     * 设置题目内容
     */
    public void setDescription(String description) {
        if (descriptionTView == null) {
            return;
        }
        descriptionTView.setText(Html.fromHtml(description));
        new ImageGetterThread(getActivity()).setHandler(imgHandler, IMG_TAG_DESCRIPTION).setHtml(description).start();
    }

    /**
     * 设置正确答案内容
     */
    public void setAnswer(String answer) {
        setAnswer(answer, true);
    }

    public void setAnswer(String answer, boolean html) {
        if (answerTView == null) {
            return;
        }
        if (html) {
            answerTView.setText(Html.fromHtml(answer));
            new ImageGetterThread(getActivity()).setHandler(imgHandler, IMG_TAG_RIGHT_ANSWER).setHtml(answer).start();
        } else {
            answerTView.setText(answer);
        }
    }

    /**
     * 设置解析内容
     */
    public void setAnalysis(String analysis) {
        if (analysisTView == null) {
            return;
        }
        analysisTView.setText(Html.fromHtml(analysis));
        new ImageGetterThread(getActivity()).setHandler(imgHandler, IMG_TAG_ANALYSIS).setHtml(analysis).start();
    }
}
